package cn.niit.dynamic;

import cn.niit.service.UserService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @title: MyInvocationHandler
 * @Author Zhj
 * @Date: 2021/10/13 10:31
 * @Version 1.0
 */
public class MyInvocationHandler {
    private UserService target;

    public MyInvocationHandler(UserService target) {
        this.target = target;
    }
    /*生成代理类*/
    public Object getProxy(){
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("*********");
                        System.out.println("getProxy开始代理....."+method.getName());
                        System.out.println("getProxy参数："+args);
                        Object returnVal =method.invoke(target,args);
                      /*  System.out.println(returnVal);*/
                        System.out.println("getProxy完成代理.....");
                        return returnVal;
                    }
                }
        );
    }
}
